package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 类描述：
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //创建一个购物车对象，将shoppingCartDTO复制到shoppingCart
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置购物车最重要的属性：用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 查询当前菜品或套餐是否在购物车中
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getShoppingCart(shoppingCart);
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            /// 如果已经存在，数量+1
            shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
        } else {
            //不存在，添加到购物车
            //尝试获取前端传过来的到底是菜品还是套餐。原理：判断哪个有数据（不为空）
            //此处用菜品查询
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //如果dishId不为空，说明是添加菜品
                Dish dish = dishMapper.getDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //如果dishId为空，则走到此处else中，说明是添加套餐.
                Integer setmealId = Math.toIntExact(shoppingCart.getSetmealId());
                SetmealVO setmealVO = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.saveCart(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> getShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getShoppingCart(shoppingCart);
        return shoppingCarts;
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //获取购物车中当前操作的菜品或套餐的数量
        Integer number = shoppingCartMapper.getNumber(shoppingCart);
        //如果是1，就清空
        if (number == 1) {
            shoppingCartMapper.cleanOne(shoppingCart);
        } else {
            //否则就减1
            number=number-1;
            shoppingCart.setNumber(number);
            shoppingCartMapper.update(shoppingCart);
        }

    }
}
